<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- integratingjs.qdoc -->
  <title>Use Case - Integrating JavaScript in QML | Qt 5.12 5.12.3</title>
  <link rel="stylesheet" type="text/css" href="style/offline-simple.css" />
  <script type="text/javascript">
    document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css");
    // loading style sheet breaks anchors that were jumped to before
    // so force jumping to anchor again
    setTimeout(function() {
        var anchor = location.hash;
        // need to jump to different anchor first (e.g. none)
        location.hash = "#";
        setTimeout(function() {
            location.hash = anchor;
        }, 0);
    }, 0);
  </script>
</head>
<body>
<div class="header" id="qtdocheader">
  <div class="main">
    <div class="main-rounded">
      <div class="navigationbar">
        <table><tr>
<td ><a href="index.html">Qt 5.12</a></td><td >Use Case - Integrating JavaScript in QML</td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right">Qt 5.12.3 参考指南</td>
        </tr></table>
      </div>
    </div>
<div class="content">
<div class="line">
<div class="content mainContent">
<div class="sidebar">
<div class="toc">
<h3><a name="toc">目录</a></h3>
<ul>
<li class="level1"><a href="#using-javascript-expressions-for-property-values">为属性值使用JavaScript表达式</a></li>
<li class="level1"><a href="#adding-javascript-functions-in-qml">在QML中添加JavaScript函数</a></li>
<li class="level1"><a href="#using-javascript-files">使用JavaScript文件</a></li>
</ul>
</div>
<div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">用例——在QML中集成JavaScript</h1>
<span class="subtitle"></span>
<!-- $$$qtquick-usecase-integratingjs.html-description -->
<div class="descr"> <a name="details"></a>
<p>JavaScript代码可以很容易地集成到QML中，以提供UI逻辑、命令控制或其他好处。</p>
<a name="using-javascript-expressions-for-property-values"></a>
<h2 id="using-javascript-expressions-for-property-values">为属性值使用JavaScript表达式</h2>
<p>JavaScript表达式可以在QML中用作绑定。例如:</p>
<pre class="cpp">

  Item {
      width: Math<span class="operator">.</span>random()
      height: width <span class="operator">&lt;</span> <span class="number">100</span> <span class="operator">?</span> <span class="number">100</span> : (width <span class="operator">+</span> <span class="number">50</span>) <span class="operator">/</span>  <span class="number">2</span>
  }

</pre>
<p>注意，像Math.random()这样的函数调用不会被重新赋值，除非它们的参数发生变化。因此，绑定到Math.random()将是一个随机数，不会重新赋值，但是如果宽度以其他方式更改，则会重新计算高度绑定，以考虑到这一点。</p>
<a name="adding-javascript-functions-in-qml"></a>
<h2 id="adding-javascript-functions-in-qml">在QML中添加JavaScript函数</h2>
<p>JavaScript函数可以在QML项上声明，如下面的示例所示。这允许您使用项id调用方法。 </p>
<pre class="qml">

  import QtQuick 2.12

  <span class="type"><a href="../qtquick/qml-qtquick-item.html">Item</a></span> {
      <span class="name">id</span>: <span class="name">container</span>
      <span class="name">width</span>: <span class="number">320</span>
      <span class="name">height</span>: <span class="number">480</span>

      <span class="keyword">function</span> <span class="name">randomNumber</span>() {
          <span class="keyword">return</span> <span class="name">Math</span>.<span class="name">random</span>() <span class="operator">*</span> <span class="number">360</span>;
      }

      <span class="keyword">function</span> <span class="name">getNumber</span>() {
          <span class="keyword">return</span> <span class="name">container</span>.<span class="name">randomNumber</span>();
      }

      <span class="type"><a href="../qtquick/qml-qtquick-taphandler.html">TapHandler</a></span> {
          <span class="comment">// This line uses the JS function from the item</span>
          <span class="name">onTapped</span>: <span class="name">rectangle</span>.<span class="name">rotation</span> <span class="operator">=</span> <span class="name">container</span>.<span class="name">getNumber</span>();
      }

      <span class="type"><a href="../qtquick/qml-qtquick-rectangle.html">Rectangle</a></span> {
          <span class="name">color</span>: <span class="string">&quot;#272822&quot;</span>
          <span class="name">width</span>: <span class="number">320</span>
          <span class="name">height</span>: <span class="number">480</span>
      }

      <span class="type"><a href="../qtquick/qml-qtquick-rectangle.html">Rectangle</a></span> {
          <span class="name">id</span>: <span class="name">rectangle</span>
          <span class="name">anchors</span>.centerIn: <span class="name">parent</span>
          <span class="name">width</span>: <span class="number">160</span>
          <span class="name">height</span>: <span class="number">160</span>
          <span class="name">color</span>: <span class="string">&quot;green&quot;</span>
          Behavior on <span class="name">rotation</span> { <span class="type"><a href="../qtquick/qml-qtquick-rotationanimation.html">RotationAnimation</a></span> { <span class="name">direction</span>: <span class="name">RotationAnimation</span>.<span class="name">Clockwise</span> } }
      }

  }

</pre>
<a name="using-javascript-files"></a>
<h2 id="using-javascript-files">使用JavaScript文件</h2>
<p>JavaScript文件可用于从QML文件中抽象出逻辑。为此，首先将函数放在.js文件中，如下例所示。</p>
<pre class="js">

  <span class="comment">// myscript.js</span>
  <span class="keyword">function</span> <span class="name">getRandom</span>(previousValue) {
      <span class="keyword">return</span> <span class="name">Math</span>.<span class="name">floor</span>(<span class="name">previousValue</span> <span class="operator">+</span> <span class="name">Math</span>.<span class="name">random</span>() <span class="operator">*</span> <span class="number">90</span>) <span class="operator">%</span> <span class="number">360</span>;
  }

</pre>
<p>然后将该文件导入需要使用这些函数的任何. QML文件，如下面的示例QML文件所示。</p>
<pre class="qml">

  import QtQuick 2.12
  import &quot;myscript.js&quot; as Logic

  <span class="type"><a href="../qtquick/qml-qtquick-item.html">Item</a></span> {
      <span class="name">width</span>: <span class="number">320</span>
      <span class="name">height</span>: <span class="number">480</span>

      <span class="type"><a href="../qtquick/qml-qtquick-rectangle.html">Rectangle</a></span> {
          <span class="name">color</span>: <span class="string">&quot;#272822&quot;</span>
          <span class="name">width</span>: <span class="number">320</span>
          <span class="name">height</span>: <span class="number">480</span>
      }

      <span class="type"><a href="../qtquick/qml-qtquick-taphandler.html">TapHandler</a></span> {
          <span class="comment">// 这一行使用来自单独JS文件的JS函数</span>
          <span class="name">onTapped</span>: <span class="name">rectangle</span>.<span class="name">rotation</span> <span class="operator">=</span> <span class="name">Logic</span>.<span class="name">getRandom</span>(<span class="name">rectangle</span>.<span class="name">rotation</span>);
      }

      <span class="type"><a href="../qtquick/qml-qtquick-rectangle.html">Rectangle</a></span> {
          <span class="name">id</span>: <span class="name">rectangle</span>
          <span class="name">anchors</span>.centerIn: <span class="name">parent</span>
          <span class="name">width</span>: <span class="number">160</span>
          <span class="name">height</span>: <span class="number">160</span>
          <span class="name">color</span>: <span class="string">&quot;green&quot;</span>
          Behavior on <span class="name">rotation</span> { <span class="type"><a href="../qtquick/qml-qtquick-rotationanimation.html">RotationAnimation</a></span> { <span class="name">direction</span>: <span class="name">RotationAnimation</span>.<span class="name">Clockwise</span> } }
      }

  }

</pre>
<p class="centerAlign"><img src="images/qml-uses-integratingjs.png" alt="" /></p><p>有关QML使用的JavaScript引擎的更多细节，以及与浏览器JS的区别，请参阅<a href="../qtqml/qtqml-javascript-expressions.html">QML文档中关于JavaScript表达式</a>的完整文档。</p>
</div>
<!-- @@@qtquick-usecase-integratingjs.html -->
        </div>
       </div>
   </div>
   </div>
</div>
<div class="footer">
   <p>
   <acronym title="Copyright">&copy;</acronym> 2019 The Qt Company Ltd.
   Documentation contributions included herein are the copyrights of
   their respective owners.<br/>    The documentation provided herein is licensed under the terms of the    <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation    License version 1.3</a> as published by the Free Software Foundation.<br/>    Qt and respective logos are trademarks of The Qt Company Ltd.     in Finland and/or other countries worldwide. All other trademarks are property
   of their respective owners. </p>
</div>
</body>
</html>
